package vn.com.bookstore.service;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.com.bookstore.entity.impl.Book;


@Service("bookServices")
@Transactional
public class BookServices {
	protected static Logger logger = Logger.getLogger("service");
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	public BookServices()
	{
	}
	
	public void Add(Book book)
	{
		logger.debug("Adding new book");
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		// Persists to db
		session.save(book);
	}
	
	public void Delete(Integer id)
	{
		logger.debug("Deleting book id=" + id);
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM Book WHERE Book.bookId="+id);
		// Retrieve record
		Book book = (Book) query.uniqueResult();
		// Delete person
		session.delete(book);
	}
}
